import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';
import 'package:hc_ui/config/hc_customer_config.dart';
import 'package:hc_ui/config/hc_default_theme_config.dart';
import 'package:hc_ui/theme/hc_base_theme.dart';

import 'hc_common_theme.dart';

class HcCellTheme extends HcBaseTheme<HcCellTheme> {
  Border? _border;
  BorderRadius? _borderRadius;
  Color? _backgroundColor;
  EdgeInsets? _cellListPadding;
  EdgeInsets? _margin;

  EdgeInsets? _cellGridPadding;
  double? _cellLeadingSize;

  EdgeInsets? _leadingPadding;
  EdgeInsets? _leadingMargin;
  double? _leadingSize;
  Color? _leadingColor;

  EdgeInsets? _trailingMargin;
  double? _trailingSize;
  Color? _trailingColor;

  EdgeInsets? _titlePadding;
  TextStyle? _titleTextStyle;
  TextStyle? _subtitleTextStyle;

  EdgeInsets? _actionMargin;

  double? _singleSubtitleSpacing;
  double? _multipleSubtitleSpacing;

  HcCellTheme(
      {Border? border,
      BorderRadius? borderRadius,
      Color? backgroundColor,
      EdgeInsets? cellListPadding,
      EdgeInsets? margin,
      EdgeInsets? leadingPadding,
      EdgeInsets? leadingMargin,
      EdgeInsets? trailingMargin,
      EdgeInsets? cellGridPadding,
      EdgeInsets? titlePadding,
      EdgeInsets? actionMargin,
      double? singleSubtitleSpacing,
      double? multipleSubtitleSpacing,
      double? leadingSize,
      double? cellLeadingSize,
      double? trailingSize,
      Color? leadingColor,
      Color? trailingColor,
      TextStyle? titleTextStyle,
      TextStyle? subtitleTextStyle})
      : _border = border,
        _borderRadius = borderRadius,
        _backgroundColor = backgroundColor,
        _cellListPadding = cellListPadding,
        _margin = margin,
        _leadingPadding = leadingPadding,
        _leadingMargin = leadingMargin,
        _trailingMargin = trailingMargin,
        _cellGridPadding = cellGridPadding,
        _titlePadding = titlePadding,
        _actionMargin = actionMargin,
        _singleSubtitleSpacing = singleSubtitleSpacing,
        _multipleSubtitleSpacing = multipleSubtitleSpacing,
        _leadingSize = leadingSize,
        _cellLeadingSize = cellLeadingSize,
        _trailingSize = trailingSize,
        _leadingColor = leadingColor,
        _trailingColor = trailingColor,
        _titleTextStyle = titleTextStyle,
        _subtitleTextStyle = subtitleTextStyle;

  @override
  void initTheme({HcCommonTheme? tempTheme}) {
    super.initTheme(tempTheme: tempTheme);
    HcCellTheme theme = HcCustomerConfig().allTheme.cellTheme;
    _border ??= theme.border;
    _borderRadius ??= BorderRadius.circular(commonTheme.radiusBase);
    _backgroundColor ??= commonTheme.fillWidgetColor;
    _cellListPadding ??=
        EdgeInsets.symmetric(horizontal: commonTheme.hSpacingPage);
    _margin ??= theme.margin;
    _leadingPadding ??= EdgeInsets.only(right: commonTheme.hSpacingXxs);
    _leadingMargin ??= EdgeInsets.only(right: commonTheme.hSpacingXs);
    _trailingMargin ??= EdgeInsets.only(left: commonTheme.hSpacingBase);
    _titlePadding ??= EdgeInsets.symmetric(vertical: commonTheme.vSpacingPage);
    _cellGridPadding ??=
        EdgeInsets.symmetric(vertical: commonTheme.vSpacingBase);
    _titleTextStyle ??= TextStyle(
        fontSize: commonTheme.fontSizeSubtitle,
        color: commonTheme.textTitleColor,
        height: 1.4);
    _subtitleTextStyle ??= TextStyle(
        fontSize: commonTheme.fontSizeBase,
        color: commonTheme.textSubtitleColor);
    _singleSubtitleSpacing ??= commonTheme.vSpacingSm;
    _multipleSubtitleSpacing ??= commonTheme.vSpacingXxs;
    _actionMargin ??= EdgeInsets.only(left: commonTheme.hSpacingXs);
    _leadingColor ??= commonTheme.textSubtitleColor;
    _trailingColor ??= commonTheme.textDisableColor;
    _leadingSize ??= commonTheme.iconSizeSm;
    _cellLeadingSize ??= commonTheme.iconSizeXxl;
    _trailingSize ??= commonTheme.iconSizeXxs;
  }

  @override
  HcCellTheme merge(HcCellTheme? value) {
    if (value == null) return this;
    return copyWith(
      border: value._border,
      borderRadius: value._borderRadius,
      backgroundColor: value._backgroundColor,
      margin: value._margin,
      cellListPadding: value._cellListPadding,
      leadingPadding: value._leadingPadding,
      leadingMargin: value._leadingMargin,
      trailingMargin: value._trailingMargin,
      cellGridPadding: value._cellGridPadding,
      titlePadding: value._titlePadding,
      actionMargin: value._actionMargin,
      singleSubtitleSpacing: value._singleSubtitleSpacing,
      multipleSubtitleSpacing: value._multipleSubtitleSpacing,
      leadingSize: value._leadingSize,
      cellLeadingSize: value._cellLeadingSize,
      trailingSize: value._trailingSize,
      leadingColor: value._leadingColor,
      titleTextStyle: titleTextStyle.merge(value._titleTextStyle),
      subtitleTextStyle: subtitleTextStyle.merge(value._subtitleTextStyle),
    );
  }

  HcCellTheme copyWith(
      {Border? border,
      BorderRadius? borderRadius,
      Color? backgroundColor,
      EdgeInsets? margin,
      EdgeInsets? cellListPadding,
      EdgeInsets? leadingPadding,
      EdgeInsets? leadingMargin,
      EdgeInsets? trailingMargin,
      EdgeInsets? cellGridPadding,
      EdgeInsets? titlePadding,
      EdgeInsets? actionMargin,
      double? singleSubtitleSpacing,
      double? multipleSubtitleSpacing,
      double? leadingSize,
      double? cellLeadingSize,
      double? trailingSize,
      Color? leadingColor,
      Color? trailingColor,
      TextStyle? titleTextStyle,
      TextStyle? subtitleTextStyle}) {
    return HcCellTheme(
      border: border ?? _border,
      borderRadius: borderRadius ?? _borderRadius,
      backgroundColor: backgroundColor ?? _backgroundColor,
      margin: margin ?? _margin,
      cellListPadding: cellListPadding ?? _cellListPadding,
      leadingPadding: leadingPadding ?? _leadingPadding,
      leadingMargin: leadingMargin ?? _leadingMargin,
      titlePadding: titlePadding ?? _titlePadding,
      actionMargin: actionMargin ?? _actionMargin,
      singleSubtitleSpacing: singleSubtitleSpacing ?? _singleSubtitleSpacing,
      multipleSubtitleSpacing:
          multipleSubtitleSpacing ?? _multipleSubtitleSpacing,
      cellGridPadding: cellGridPadding ?? _cellGridPadding,
      leadingColor: leadingColor ?? _leadingColor,
      trailingColor: trailingColor ?? _trailingColor,
      leadingSize: leadingSize ?? _leadingSize,
      cellLeadingSize: cellLeadingSize ?? _cellLeadingSize,
      trailingSize: trailingSize ?? _trailingSize,
      titleTextStyle: titleTextStyle ?? _titleTextStyle,
      subtitleTextStyle: subtitleTextStyle ?? _subtitleTextStyle,
    );
  }

  TextStyle get subtitleTextStyle =>
      _subtitleTextStyle ??
      HcDefaultThemeConfig.defaultCellTheme.subtitleTextStyle;

  TextStyle get titleTextStyle =>
      _titleTextStyle ?? HcDefaultThemeConfig.defaultCellTheme.titleTextStyle;

  EdgeInsets get cellListPadding =>
      _cellListPadding ?? HcDefaultThemeConfig.defaultCellTheme.cellListPadding;

  Color get leadingColor =>
      _leadingColor ?? HcDefaultThemeConfig.defaultCellTheme.leadingColor;

  double get leadingSize =>
      _leadingSize ?? HcDefaultThemeConfig.defaultCellTheme.leadingSize;

  double get cellLeadingSize =>
      _cellLeadingSize ?? HcDefaultThemeConfig.defaultCellTheme.cellLeadingSize;

  EdgeInsets get leadingMargin =>
      _leadingMargin ?? HcDefaultThemeConfig.defaultCellTheme.leadingMargin;

  double get singleSubtitleSpacing =>
      _singleSubtitleSpacing ??
      HcDefaultThemeConfig.defaultCellTheme.singleSubtitleSpacing;

  double get multipleSubtitleSpacing =>
      _multipleSubtitleSpacing ??
      HcDefaultThemeConfig.defaultCellTheme.multipleSubtitleSpacing;

  EdgeInsets get titlePadding =>
      _titlePadding ?? HcDefaultThemeConfig.defaultCellTheme.titlePadding;

  EdgeInsets get cellGridPadding =>
      _cellGridPadding ?? HcDefaultThemeConfig.defaultCellTheme.cellGridPadding;

  EdgeInsets get trailingMargin =>
      _trailingMargin ?? HcDefaultThemeConfig.defaultCellTheme.trailingMargin;

  EdgeInsets get leadingPadding =>
      _leadingPadding ?? HcDefaultThemeConfig.defaultCellTheme.leadingPadding;

  EdgeInsets get actionMargin =>
      _actionMargin ?? HcDefaultThemeConfig.defaultCellTheme.actionMargin;

  double get trailingSize =>
      _trailingSize ?? HcDefaultThemeConfig.defaultCellTheme.trailingSize;

  EdgeInsets get margin =>
      _margin ?? HcDefaultThemeConfig.defaultCellTheme.margin;

  Color get backgroundColor =>
      _backgroundColor ?? HcDefaultThemeConfig.defaultCellTheme.backgroundColor;

  BorderRadius get borderRadius =>
      _borderRadius ?? HcDefaultThemeConfig.defaultCellTheme.borderRadius;

  Border get border => _border ?? HcDefaultThemeConfig.defaultCellTheme.border;

  Color get trailingColor =>
      _trailingColor ?? HcDefaultThemeConfig.defaultCellTheme.trailingColor;
}
